const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    coach: null,
    canClick: true,
    appointmentDate: '',
    appointmentTime: '',
    codeText: '获取验证码',
    nickName: '',
    phone: '',
    code: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const coachJson = wx.getStorageSync("coach");
    const coach = JSON.parse(coachJson);
    const appointmentTime = wx.getStorageSync("appointmentTime");
    const appointmentDate = wx.getStorageSync("appointmentDate");
    this.setData({
      coach: coach,
      appointmentDate: appointmentDate,
      appointmentTime: appointmentTime
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      canClick: true
    });
  },
  setNickName: function(e) {
    this.setData({
      nickName: e.detail.value
    })
  },
  setPhone: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  setCode: function(e) {
    this.setData({
      code: e.detail.value
    })
  },
  confirm: function() {
    const that = this;
    const coach = this.data.coach;
    const nickName = this.data.nickName;
    const phone = this.data.phone;
    const code = this.data.code;
    const openId = app.globalData.openId;
    const url = app.globalData.url;
    const userInfo = app.globalData.userInfo;
    const appointmentDate = this.data.appointmentDate;
    const appointmentTime = this.data.appointmentTime;
    const appointmentTimeArr = appointmentTime.split('-');
    if (nickName.length === 0) {
      wx.showToast({
        title: '请输入昵称',
        icon: 'none'
      });
      return;
    }
    if (phone.length === 0) {
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none'
      });
      return;
    }
    if (code.length === 0) {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none'
      });
      return;
    }
    this.setData({
      canClick: false
    });
    wx.request({
      url: url + '/appointment/save',
      data: {
        traineeOpenId: openId,
        traineeName: nickName,
        traineePhone: phone,
        code: code,
        traineeAvatarUrl: userInfo.avatarUrl,
        coachOpenId: coach.openId,
        coachAvatarUrl: coach.avatarUrl,
        coachName: coach.name,
        appointmentDate: appointmentDate,
        appointmentTimeStart: appointmentTimeArr[0],
        appointmentTimeEnd: appointmentTimeArr[1]
      },
      method: 'POST',
      success: function(response) {
        if (response.data.success) {
          wx.navigateBack({
            delta: 2
          });
          wx.showToast({
            title: '提交成功',
            icon: 'none'
          });
        } else {
          that.setData({
            canClick: true
          });
          wx.showToast({
            title: '提交失败:' + response.data.message,
            icon: 'none'
          });
        }
      },
      fail: function(res) {
        wx.showToast({
          title: '微信请求失败,请重试',
          icon: 'none'
        });
        this.setData({
          canClick: true
        });
      }
    })
  },
  getCode: function() {
    const phone = this.data.phone;
    const url = app.globalData.url;
    if (phone.length === 0) {
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none'
      });
      return;
    }
    wx.request({
      url: url + '/management/traineeCode/' + phone,
      success: function(response) {
        if (response.data.success) {
          wx.showToast({
            title: '验证码已经发送,请注意查看',
            icon: 'none'
          });
        }else{
          wx.showToast({
            title: '提取验证码失败',
            icon: 'none'
          });
        }
      }
    })
  }
})